package com.dom.domweb.modular.controller.sysmger;

import com.dom.common.constants.ConstantError;
import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.common.TabPager;
import com.dom.domweb.dao.entity.TRoleInfo;
import com.dom.domweb.dao.service.ITRoleInfoService;
import com.dom.domweb.modular.shiro.model.ShiroUser;
import com.dom.domweb.modular.shiro.util.ShiroUtil;
import com.xiaoleilu.hutool.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 角色信息控制层代码
 * @Author xllei
 * @Date 2018/11/22 11:24 AM
 * @Version 1.0
 **/
@RestController
@RequestMapping("/sys")
public class RoleController {
    @Autowired
    private ITRoleInfoService roleInfoService;

    @RequestMapping("/getRoleList")
    public RspModel getRoleList(){
        ShiroUser user = ShiroUtil.getUser();
        if(ObjectUtil.isNull(user.getMemCode())){
            return new RspModel(ConstantError.BLANKMEMCODEERROR);
        }

        List<TRoleInfo> roleInfos = roleInfoService.findRoleList(user.getMemCode());
        return new RspModel(roleInfos);
    }

    @RequestMapping("/getRolePager")
    public TabPager getRolePager(@RequestParam("limit")int limit, @RequestParam("page")int page,
                                  @RequestParam("roleName") String roleName,
                                  @RequestParam("stat")Integer stat){
        ShiroUser user = ShiroUtil.getUser();

        B089R request = new B089R();
        request.setPn(page);
        request.setPs(limit);
        request.setRoleName(roleName);
        request.setStat(stat);

        B089A response = roleInfoService.findRoleByPager(request, user);
        return new TabPager(response.getRoleInfoData(), response.getTotalNum());
    }

    @RequestMapping("/addRoleInfo")
    public RspModel addRoleInfo(@RequestParam("roleName") String roleName) {
        B080R request = new B080R();
        request.setRoleName(roleName);

        ShiroUser user = ShiroUtil.getUser();
        request.setMemCode(user.getMemCode());

        B080A response = roleInfoService.addRoleInfo(request);
        return new RspModel(response.getRoleId());
    }

    @RequestMapping("/editRoleInfo")
    public RspModel editRoleInfo(@RequestParam("id")Integer id, @RequestParam("roleName")String roleName,
                                 @RequestParam("stat")Integer stat){
        B082R request = new B082R();
        if(0 == id){
            return new RspModel("RSP090001", "请选择角色信息修改");
        }
        request.setRoleId(id);
        request.setRoleName(roleName);
        request.setStat(stat);

        B082A response = roleInfoService.editRoleInfo(request);
        return new RspModel(response.getRoleId());
    }

    @RequestMapping("/delRoleInfo")
    public RspModel delRoleInfo(@RequestParam("id") Integer id){
        if(0 == id){
            return new RspModel("RSP090001", "请选择机构信息修改");
        }

        B081R request = new B081R();
        request.setRoleId(id);

        B081A response = roleInfoService.delRoleInfo(request);
        return new RspModel(response.getRoleId());
    }
}
